#include "widget.h"
#include "./ui_widget.h"


Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
    //播放器设置
    , player(new QMediaPlayer(this)), audioOutPut(new QAudioOutput(this))
{
    ui->setupUi(this);
    // 关联播放器和音频输出设备
    player->setAudioOutput(audioOutPut);

    // 设置音量（0.0 ~ 1.0）
    audioOutPut->setVolume(0.5);

    // 播放背景音乐
    playBackGroundMusic("C:/Users/Annie Tang/Music/ko-ko-ya - 小鳥のワルツ.flac");


    // 创建四个按钮
    button1 = new QPushButton("sad", this);
    button2 = new QPushButton("happy", this);
    button3 = new QPushButton("anxious", this);
    button4 = new QPushButton("angry", this);

    // 设置按钮的位置
    button1->move(285, 140);
    button2->move(268, 369);
    button3->move(504, 407);
    button4->move(531, 157);


    // 显示按钮
    button1->show();
    button2->show();
    button3->show();
    button4->show();

    // 连接按钮的点击信号到各自的槽函数
    connect(button1, &QPushButton::clicked, this, &Widget::openGameWindow1);
    connect(button2, &QPushButton::clicked, this, &Widget::openGameWindow3);
    connect(button3, &QPushButton::clicked, this, &Widget::openGameWindow4);
    connect(button4, &QPushButton::clicked, this, &Widget::openGameWindow2);
}

Widget::~Widget()
{
    delete ui;
    delete player;
    delete audioOutPut;
}

//使用函数paintEvent显示背景图片
void Widget::paintEvent(QPaintEvent *event)
{
    QPainter painter(this);
    QPixmap pixmap("D:/QtCreator/HeartIslandTest/figures/background pic.jpg");
    //这里放了背景图路径

    QPixmap scaledPixmap = pixmap.scaled(size(), Qt::KeepAspectRatioByExpanding, Qt::SmoothTransformation);

    int x = (width() - scaledPixmap.width()) / 2;
    int y = (height() - scaledPixmap.height()) / 2;

    painter.drawPixmap(x, y, scaledPixmap);
}



void Widget::playBackGroundMusic(const QString &filePath) {
    player->setSource(QUrl::fromLocalFile(filePath));  // 设置音乐文件
    player->play();  // 开始播放
}
void Widget::openGameWindow1() {
    qDebug() << "Button 1 clicked!";
    this->hide();
    this->muteMusic();
    gameWindow=new GameWindow();
    gameWindow->setAttribute(Qt::WA_DeleteOnClose);
    connect(gameWindow, &GameWindow::gameWindowClosed, this, [this]() {
        qDebug()<<"closing gamewindow and showing widget";
        gameWindow=nullptr;
        this->show();
        this->unmuteMusic();        // GameWindow 关闭后，重新显示主窗口
    });
    gameWindow->show();
    }

void Widget::openGameWindow2() {
    qDebug() << "Button 2 clicked!";
        this->hide();
        this->muteMusic();
        gameWindow2=new GameWindow2();
        gameWindow2->setAttribute(Qt::WA_DeleteOnClose);
        connect(gameWindow2,&GameWindow2::gameWindow2Closed,this,[this](){
            qDebug()<<"closing gamewindow and showing widget";
            gameWindow2=nullptr;
            this->show();
            this->unmuteMusic();
        });
    gameWindow2->show();
    }

void Widget::openGameWindow3() {
    qDebug() << "Button 3 clicked!";
    this->hide();
    this->muteMusic();
    gameWindow3=new GameWindow3();
    gameWindow3->setAttribute(Qt::WA_DeleteOnClose);
    connect(gameWindow3,&GameWindow3::gameWindow3Closed,this,[this](){
        qDebug()<<"closing gamewindow and showing widget";
        gameWindow3=nullptr;
        this->show();
        this->unmuteMusic();
    });
    gameWindow3->show();
}

void Widget::openGameWindow4() {
    qDebug() << "Button 3 clicked!";
    this->hide();
    this->muteMusic();
    gameWindow4=new GameWindow4();
    gameWindow4->setAttribute(Qt::WA_DeleteOnClose);
    connect(gameWindow4,&GameWindow4::gameWindow4Closed,this,[this](){
        qDebug()<<"closing gamewindow and showing widget";
        gameWindow4=nullptr;
        this->show();
        this->unmuteMusic();
    });
    gameWindow4->show();
}

void Widget::resizeEvent(QResizeEvent *event) {
    if (event->oldSize().width() == -1) {
        // 窗口刚创建时，不调整，保持原来的 move 位置
        return;
    }
    int w = this->width();
    int h = this->height();

    button1->move(w * 0.3, h * 0.2);  // 按照窗口大小的比例调整位置
    button2->move(w * 0.28, h * 0.7);
    button3->move(w * 0.55, h * 0.75);
    button4->move(w * 0.6, h * 0.2);
}
void Widget::muteMusic() {
    audioOutPut->setMuted(true);
}

void Widget::unmuteMusic() {
    audioOutPut->setMuted(false);
}




